package br.com.gsc.vaadin;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Map;

import com.invient.vaadin.charts.InvientCharts;
import com.invient.vaadin.charts.InvientCharts.Series;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Layout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public abstract class GenericGraphWithRightMenu<K,V> extends Window{

private static final long serialVersionUID = -3602614710561158270L;
	
	private VerticalLayout chartLayout;
	private HorizontalLayout horzMain;
	private VerticalLayout vertMenu;
	private Panel menuPanel;
	private VerticalLayout menuLayout;
	private InvientCharts chart;
	private Button howLink;
	private ThemeResource TARGET;	
	private static final ThemeResource ICON = new ThemeResource("img/question_mark.png");
	private Map<K,V> mapOfBugsFiltered;
	private Map<K,V> mapFiltered;
	private int helpWindowWidth;
	private int helpWindowHeight;
	private Label helpLabel;
	
	
	public GenericGraphWithRightMenu(){
    	horzMain = new HorizontalLayout();    	
        chartLayout = new VerticalLayout();
        vertMenu = new VerticalLayout();
//        vertMenu.addComponent(setupFaq());
//        vertMenu.setComponentAlignment(howLink, Alignment.TOP_RIGHT);
        menuLayout = new VerticalLayout();
	}
	
    public void createComponents(Map<K,V> map, Map<K,V> mapFiltered) {
    	
    	this.mapOfBugsFiltered = map;
    	this.mapFiltered = mapFiltered;
		
        menuPanel = new Panel(); 
        
        /**
         * Chart
         */
        chart = setupChart(mapFiltered);
        chartLayout.setSizeFull();
        chartLayout.addComponentAsFirst(chart);
        chartLayout.setExpandRatio(chart, 1.0f);
        
        /**
         * Right side Menu
         */                      
        vertMenu.setSpacing(true);
        vertMenu.setSizeFull();           
        menuPanel.addComponent(vertMenu); 
        menuPanel.setSizeFull();
        menuPanel.getContent().setSizeFull();     
        menuLayout.addComponent(menuPanel);
        menuLayout.setWidth(220, UNITS_PIXELS);
        menuLayout.setHeight(100, UNITS_PERCENTAGE);      
        
        /**
         * Layout superior a todos os components
         */
        horzMain.setSpacing(true);
        horzMain.setMargin(true);
        horzMain.addComponent(chartLayout);
        horzMain.addComponent(menuLayout);
        horzMain.setExpandRatio(chartLayout, 1.0f); 
        horzMain.setSizeFull();
        
        /**
         * Janela
         */
        this.setResizable(true);
        this.setImmediate(true);
        this.setHeight(600,UNITS_PIXELS);   
        this.setWidth(900, UNITS_PIXELS);
        this.setContent(horzMain);        
        this.setVisible(true);
        this.addListener(new Window.ResizeListener() { // so the graph gets resized together with the window

			private static final long serialVersionUID = 1L;

			public void windowResized(ResizeEvent e) {
				resizeGraph(true);
				horzMain.requestRepaintAll();								
			}
		});

    }

	private Button setupFaq() {
		howLink = new Button();    
        howLink.setIcon(ICON);
        howLink.addStyleName("borderless");
        howLink.addListener(new Button.ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				
				showPopupHelp();
			}
		});
		return howLink;
	}
	
	private void showPopupHelp(){
		Window help = new Window();
		help.setModal(true);
		help.setWidth(helpWindowWidth, UNITS_PIXELS);
		help.setHeight(helpWindowHeight, UNITS_PIXELS);
		help.addComponent(helpLabel);
		this.getApplication().getMainWindow().addWindow(help);
	}
    
    protected void setHelpMessage(String message, int width, int height){
    	helpWindowHeight = height;
    	helpWindowWidth = width;
    	helpLabel = new Label(message);
    	helpLabel.setContentMode(Label.CONTENT_XHTML);    	
    }
	
    protected void setExpandComponent(Component comp){
    	vertMenu.setExpandRatio(comp, 1.0f);
    }
    
	protected void addComponentsToRightMenu(Component comp){
		vertMenu.addComponent(comp);
		vertMenu.setComponentAlignment(comp, Alignment.TOP_CENTER);
	}
	
	protected void removeComponentsFromRightMenu(Component comp){
		vertMenu.removeComponent(comp);
	}
	
	protected void removeRightMenu(){
		horzMain.removeComponent(menuLayout);
		horzMain.requestRepaintAll();
	}
	
	protected Layout getMenuLayout(){
		return vertMenu;
	}
	
	protected Layout getChartLayout(){
		return chartLayout;
	}
	
	protected abstract InvientCharts setupChart(Map<K,V> map);	

	protected void updateChartWithfilteredData(Map<K,V> filteredMap){
		
		mapFiltered = filteredMap;

		chart = setupChart(filteredMap);
				
		chartLayout.removeAllComponents();
		chartLayout.addComponentAsFirst(chart);		
	}
	
	protected void resetGraph(){
		this.resizeGraph(false);
	}
	
	
	protected void resizeGraph(boolean filtered){
		if(!filtered){
			chart = setupChart(mapOfBugsFiltered);
		}else{
			chart = setupChart(mapFiltered);
		}					
		chartLayout.removeAllComponents();
		chartLayout.addComponentAsFirst(chart);
		chartLayout.requestRepaint();		
	}
	
    public static long getPointStartDate(int year, int month, int day) {
        Calendar cal = GregorianCalendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, day);
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }
    
    protected void addSeriesToChart(Series series){
    	chart.addSeries(series);
    }
   
}
